import rclpy
from rclpy.node import Node
from my_interfaces.msg import MyHello


class Topic_Subscriber(Node):
    def __init__(self, name):
        super().__init__(name)
        self.create_subscription(MyHello, "chat_hello", self.listen_callback, 10)       # 创建订阅者并链接回调函数
        self.get_logger().info("Init Success ! & Start Listening ...")
    
    def listen_callback(self, msg):                                                     # 回调函数以 msg对象作为参数
        self.get_logger().info("Already get data: %s - %d" % (msg.name, msg.id))        # 调取 msg对象的各项属性


def main(args=None):
    rclpy.init(args=args)

    node = Topic_Subscriber("topic_hello_sub")
    rclpy.spin(node)

    node.destroy_node()
    rclpy.shutdown()